package com.auth.service.impl;

import com.auth.mapper.BsRolePermissionMapper;
import com.auth.model.BsRolePermission;
import com.auth.service.BsRolePermissionService;
import com.exception.bean.Result;
import com.exception.enu.ExceptionEnum;
import com.exception.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/8/22 0022.角色权限逻辑类
 */
@Service
public class BsRolePermissionServiceImpl implements BsRolePermissionService {

    @Autowired
    private BsRolePermissionMapper bsRolePermissionMapper;

    /**
     * 创建角色权限
     * @param roleId 角色ID
     * @param type   类型
     * @param menuId 菜单ID
     * @param itemId 功能项ID
     * @return
     * @throws Exception
     */
    public Result createBsRolePermission(String roleId, int type, String menuId, String itemId,String unitiId) throws Exception {
        BsRolePermission bsRolePermission=new BsRolePermission();
        bsRolePermission.setId(UUID.randomUUID().toString());
        bsRolePermission.setRid(roleId);
        bsRolePermission.setMenuid(menuId);
        bsRolePermission.setType(type);
        bsRolePermission.setItemid(itemId);
        bsRolePermission.setUnitid(unitiId);
        return ResultUtil.success(bsRolePermissionMapper.insertSelective(bsRolePermission));
    }

    /**
     * 修改角色权限
     * @param bsRolePermission
     * @return
     * @throws Exception
     */
    public Result updateBsRolePermission(BsRolePermission bsRolePermission) throws Exception {
        return ResultUtil.success(bsRolePermissionMapper.updateByPrimaryKey(bsRolePermission));
    }

    /**
     * 查询角色权限列表
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public List<BsRolePermission> findAllPage(int pageNum, int pageSize) throws Exception {
        PageHelper.offsetPage(pageNum, pageSize);
        List<BsRolePermission> list =bsRolePermissionMapper.selectAll();
        return list;
    }

    /**
     * 删除角色权限
     * @param id
     * @return
     * @throws Exception
     */
    public Result deleteBsRolePermission(String id) throws Exception {
        int i=bsRolePermissionMapper.deleteByPrimaryKey(id);
        return i>0?ResultUtil.success():ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
    }
}
